import * as API from "../../utils/config.js";
let ajaxcommon = require("../../utils/ajaxcommon.js");
const app = getApp()

Page({
  data: {
    list: [],
    List:[],
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    indicatorDots: true,
    autoplay: true,
    circular:true,
    interval: 2000,
    duration: 1000,
    
  },
  onReady: function () {
    // wx.getStorage({
    //   key: 'lis',
    //   success: function(res) {
    //     // console.log(res.data)
    //   },
    // })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    this._initData()
    // this._ListinitData()
    if (this.data.List.length == 0) {
      wx.getStorage({
        key: 'bookyoungMan',
        success: (res) => {
          // console.log(res.data)
          if (res) {
            this.setData({
              List: res.data
            })
          }
        },
        fail: (res) => {
          // console.log(this)
          this._ListinitData()
        }
      })
    }
  },
  _initData() {
    // console.log(API)
    wx.request({
      url: API.getR,
      // data:{
      //   "key":"7e3032ef818610a8111178b804c39ac2"
      // },
      success: (res) => {
        this.setData({
          list: res.data.data
          
        })
        // console.log(this.data.list)
      }
    })
  },
  _ListinitData() {
    // console.log(API.getM)
    wx.request({
      url: API.book,
      data:{
        "key":"7e3032ef818610a8111178b804c39ac2"
      },
      success: (res) => {
        wx.setStorage({
          key: 'bookyoungMan',
          data: res.data.result.bookList
        }),
        this.setData({
          List: res.data.result.bookList
        })
        // console.log(this.data.List)
      }
    })
  },
  to:function(e){
    let self = this;
    wx.navigateTo({
      url: "../contents/content?name=" + e.currentTarget.dataset.name
    })
  }
})